<?php

class ZolAction extends Action {
    var $PickRoot= "";
    function __construct(){
        $this->PickRoot=PICK_DIR.MODULE_NAME.DIRECTORY_SEPARATOR;
        if(!is_dir($this->PickRoot)){
            mkdir($this->PickRoot);
        }
        load("@.dom");
//         passthru("chcp 65001");
    
    }
    function __destruct(){
      
//         passthru("chcp 936");
//         logCmd("__destruct");
    }
    
    
  	
    function ShowCount(){
    	$Classes = M("Classes");
    	$ClassesHasTagesInfo =M("ClassesHasTagesInfo");
    	foreach ($Classes->select() as $class){
    		$sum = $ClassesHasTagesInfo->where(array("class_id"=>$class['class_id']))->sum("count");
    		logCmd(iconv("utf-8","gbk",$class['cnname'])."=>".$sum);
    	}
    }
    
    
    
    
    
    
    function Count(){
    	$Classes = M("Classes");
    	$ClassesHasTagesInfo =M("ClassesHasTagesInfo");
    	$ProductsHasTages =M("ProductsHasTages");
    	$TagesInfo =M("TagesInfo");
    	
    	
    	foreach ($Classes->select() as $class){
    		foreach ($ClassesHasTagesInfo->where(array("class_id"=>$class['class_id']))->select() as $ctagesInfo){
    			$procount=$ProductsHasTages
    			->where("products_has_tages.tage_id =".$ctagesInfo['tage_id']." and products.class_id =".$class['class_id'])
    			->join("products on products_has_tages.product_id = products.product_id")
    			->count();
    			$TagesCnname =$TagesInfo->where(array("tage_id"=>$ctagesInfo['tage_id']))->getField("cnname");
    			if($procount>0){
    				logCmd(iconv("utf-8","gbk",$class['cnname'])."=>".iconv("utf-8","gbk",$TagesCnname)."=>".$procount);
    				$ClassesHasTagesInfo->where(array("class_id"=>$class['class_id'],"tage_id"=>$ctagesInfo['tage_id']))->setField("count",$procount);
    			}else{
    				$ClassesHasTagesInfo->where(array("class_id"=>$class['class_id'],"tage_id"=>$ctagesInfo['tage_id']))->delete();
    			}
    		}
    	}
    }
    
  
    
    public function index(){
        $url = "http://zj.zol.com.cn/";
        $html=file_get_html($url);
        
        $Classes = M("Classes");
        
        foreach ($html->find("#topSubSel li") as $li ){
            $li_id =$li->id;
            if(strpos($li_id,"_")){
                $subid = str_replace("topSubSel_", "", $li_id);
                $name= $li->plaintext;
                
                /**
					插入栏目ID
                 */
                $CLassId=0;
               if($Classes->where(array("cnname"=>iconv("gbk","utf-8",$name)))->count()>0){
               		$CLassId = $Classes->where(array("cnname"=>iconv("gbk","utf-8",$name)))->getField("class_id");
               }else{
               		$CLassId = $Classes->data(array("cnname"=>iconv("gbk","utf-8",$name)))->add();
               }
                
               
                
                logCmd($li->plaintext." => ".$subid);
                mkdir($this->PickRoot.$name.DIRECTORY_SEPARATOR);
                CmdExec("Zol sub id ".$subid." name ".$name." classid ".$CLassId);
            }
        }      
    }
    
   public function sub(){
       $subid  =$_GET['id'];
       $subName=$_GET['name'];
       $classid=$_GET['classid'];
       $url ="http://zj.zol.com.cn/proFilter/sub".$subid."_m0_gnoPrice__k_1_1_1.html";
       $html = file_get_html($url);
       
       $TagesClasses =M("TagesClasses");
       $ClassesHasTagesInfo =M("ClassesHasTagesInfo");
       $TagesInfo =M("TagesInfo");
       foreach ($html->find(".brand a") as $a){
           if($a->id!="filterManu_all"){
               $name= $a->plaintext;
               $href= $a->href;
               $id = str_replace("/proFilter/sub".$subid."_m", "", $href);
               $id = str_replace("_gnoPrice__k_1_1_1.html", "", $id);               
               $path=$this->PickRoot.$subName.DIRECTORY_SEPARATOR.$name.DIRECTORY_SEPARATOR;
               $path = str_replace("/", "\\", $path);
              
               $TagesClassId = 0;
               if($TagesClasses->where(array("cnname"=>"品牌"))->count()>0){
               		$TagesClassId =$TagesClasses->where(array("cnname"=>"品牌"))->getField("tage_class_id");
               }else{
               		$TagesClassId =$TagesClasses->data(array("cnname"=>"品牌"))->add();
               }
               $TagesInfoId= 0;
               if($TagesInfo->where(array("cnname"=>iconv("gbk","utf-8",$name)))->count()>0){
               		$TagesInfoId =$TagesInfo->where(array("cnname"=>iconv("gbk","utf-8",$name)))->getField("tage_id");
               }else{
               		$TagesInfoId =$TagesInfo->data(array("cnname"=>iconv("gbk","utf-8",$name)))->add();
               }
               
               if($ClassesHasTagesInfo->where(array("class_id"=>$classid,"tage_id"=>$TagesInfoId,"tage_class_id"=>$TagesClassId))->count() ==0 ){
               		$ClassesHasTagesInfo->data(array("class_id"=>$classid,"tage_id"=>$TagesInfoId,"tage_class_id"=>$TagesClassId))->add();
               }
               
               
               
               logCmd("Name:".$name);   
               if(!is_dir($path)){
               		mkdir($path);
               		CmdExec("Zol m sub ".$subid." m ".$id." tcId ".$TagesInfoId." classid ".$classid." path ".$path."");
               }         
              
           }
       }
   }
   
   public function m(){
       $Sub=$_GET['sub'];
       $M=$_GET['m'];
       $TagesClassId=$_GET['tcId'];
       $Path=$_GET['path']; 
       $classid=$_GET['classid'];
       $url ="http://zj.zol.com.cn/proFilter/sub".$Sub."_m".$M."_gnoPrice__k_1_1_1.html";
       $html = file_get_html($url);
       $Page = 1;
       foreach ($html->find(".pagebar a ") as $a){
           if(intval($a->plaintext)>intval($Page)){
               $Page=intval($a->plaintext) ;
           }
       }
       logCmd("Page:".$Page);
       CmdExec("Zol mPage sub ".$Sub." m ".$M." tcId ".$TagesClassId." classid ".$classid." path ".$Path." page ".$Page);
   }
   
   public function mPage(){
       $Sub=$_GET['sub'];
       $M=$_GET['m'];
       $TagesClassId=$_GET['tcId'];
       $classid=$_GET['classid'];
       $Path=$_GET['path'];
       $Page =$_GET['page'];
       file_put_contents($Path."info.csv","");
	   $TagesInfo =M("TagesInfo");
	   $ProductsHasTages =M("ProductsHasTages");
	   $Products =M("products");
       
       
       for ($p = 1;$p<intval($Page+1);$p++){
           $url ="http://zj.zol.com.cn/proFilter/sub".$Sub."_m".$M."_gnoPrice__k_1_1_".$p.".html";
           $html = file_get_html($url);
           echo "                            (".$p.")";
           
           foreach ($html->find(".result_list li h4 a") as $a){              
               $href=$a->href;
               $name=$a->plaintext;
               $hrefinfo = pathinfo($href);
               $id = str_replace("index", "", $hrefinfo['filename']);
               
               $ProductsNode= array();
               $ProductsNode['name'] = iconv("gbk","utf-8",$name) ;
               $ProductsNode['class_id']=$classid;
               $ProductsNode['urlarray']=serialize(array("zol"=>array("id"=>$id,"url"=>$href)));
               $proid = $Products->data($ProductsNode)->add();           
               $ProductsHasTages->data(array("product_id"=>$proid,"tage_id"=>$TagesClassId))->add();
               file_put_contents($Path."info.csv", "'".$id."','".$name."','".$href."'"."\n",FILE_APPEND);
               echo ".";
               echo $proid;
               echo ".";
           }
          echo "\n";
       }
   }
   
    
   
}